$(function(){
	/**
	 * 获取列表页面（行为）按钮
	 */
	var authCondition = {
			menuCode:curMenu.code,
			drawerCode:行为类型_前端组件,
			authAreaCode:授权分类_抽屉管理_列表,
			callback:action,
			con:"#datagrid-toolbar"}
	findResourcesByUserId(authCondition);
	search();
});

/**
 * 查询
 */
function search(){
	$.ajax({
		type : "GET",
		url : ctx + "/tree/drawer",
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			var setting = {
					check: {
						enable: false,
					},
					data: {
						simpleData: {
							enable: true,
							pIdKey: "pid",
						},
						key:{
							name:'name'
						}
					},
					callback: {
						onClick:getDrawer
					}
				};
			$.fn.zTree.init($("#drawerTree"), setting, data.content);
			var tree = $.fn.zTree.getZTreeObj("drawerTree");
			var node = tree.getNodeByParam("id", "1");
			tree.selectNode(node);
			if(tree.setting.callback.onClick != null){
				tree.setting.callback.onClick(null, tree.setting.treeId, node);//调用事件
			}
		}
	});
}

function getDrawer(event,treeId,treeNode){
	$.ajax({
		type : "GET",
		url : ctx + "/drawer/query",
		data :{id:treeNode.id},
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			$("#datagrid").datagrid({
				border : false,
				fitColumns : true,
				checkbox : true,
				fit : true,
				remoteSort : false,
				rownumbers : true,
				singleSelect : true,
				toolbar : "#datagrid-toolbar",
				data : data.content
			});
		}
	});
}

/**
 * 添加
 */
function add(){
	var drawerTree = $.fn.zTree.getZTreeObj("drawerTree");
	var nodes = drawerTree.getSelectedNodes();
	if(nodes.length == 0){
		message("请选中父节点");
		return;
	}
	$('#dialog').dialog({
	    title: '添加',
	    width: 400,
	    height: 250,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/drawer/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#addform").form('load',{});
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
	    		var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	var name = $("#name").textbox("getValue");
            	var value = $("#value").textbox("getValue");
            	var data = {};
            	data.name = name;
            	data.value = value;
            	data.pid = nodes[0].id;
            	$.ajax({
            		type : "POST",
            		url : ctx + "/drawer/create",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"添加成功")){
            				return;
            			}
            			var drawerTree = $.fn.zTree.getZTreeObj("drawerTree");
            			drawerTree.addNodes(nodes[0], data.content);
            			$('#datagrid').datagrid('appendRow',data.content);
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 更新
 */
function edit(){
	var row = undefined;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
	    title: '更新',
	    width: 400,
	    height: 250,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/drawer/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#name").textbox("setValue",row.name);
	    	$("#value").textbox("setValue",row.value);
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	
            	var name = $("#name").textbox("getValue");
            	var value = $("#value").textbox("getValue");
            	var data = {};
            	data.id = row.id;
            	data.name = name;
            	data.value = value;
            	$.ajax({
            		type : "POST",
            		url : ctx + "/drawer/update",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"修改成功")){
            				return;
            			}
            			
        				var drawerTree = $.fn.zTree.getZTreeObj("drawerTree");
            			var node = drawerTree.getNodeByParam("id", row.id, null);
            			node.name = data.content.name;
            			node.value = data.content.value;
            			drawerTree.updateNode(node);
            			
            			var index = $('#datagrid').datagrid('getRowIndex',row);
            			
            			$('#datagrid').datagrid('updateRow',{
            				index: index,
        					row: {name:data.content.name,value:data.content.value}
        				});
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 删除
 */
function del(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	
	$.messager.confirm('提示', '确定要删除吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/drawer/delete",
	    		data :{id : row.id},
	    		dataType : "json",
	    		success : function(data) {
	    			if(!showMsg(data,"删除成功")){
        				return;
        			}
	    			
	    			var drawerTree = $.fn.zTree.getZTreeObj("drawerTree");
        			var node = drawerTree.getNodeByParam("id", row.id, null);
        			drawerTree.removeNode(node);
    				
    				var index = $('#datagrid').datagrid('getRowIndex',row);
	    			$("#datagrid").datagrid('deleteRow',index);
	    		}
	    	});
	    }
	});
}

/**
 * 取消
 */
function unSelectTree(){
	var drawerTree = $.fn.zTree.getZTreeObj("drawerTree");
	var node = drawerTree.getNodeByParam("id", "1");
	tree.selectNode(node);
}

function unSelectDatagrid(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(row != null){
		var index = $('#datagrid').datagrid('getRowIndex',row);
		$('#datagrid').datagrid('unselectRow',index);
	}
}

/**
 * 添加事件
 *//*
$('#datagrid-toolbar #add_link').linkbutton({
	onClick:function(){
		add();
	}
});

*//**
 * 更新事件
 *//*
$('#datagrid-toolbar #edit_link').linkbutton({
	onClick:function(){
		edit();
	}
});

*//**
 * 删除事件
 *//*
$('#datagrid-toolbar #cancel_link').linkbutton({
	onClick:function(){
		del();
	}
});

*//**
 * 取消事件
 *//*
$('#datagrid-toolbar #unselect_link').linkbutton({
	onClick:function(){
		unSelectTree();
		unSelectDatagrid();
	}
});*/